package com.spark.controller;

import com.spark.Service.AccountService;
import com.spark.pojo.Account;
import com.spark.pojo.Diary;
import com.spark.pojo.RelationTagDiary;
import com.spark.pojo.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/Account")
public class AccountController {

    @Autowired
    private AccountService accountService;

    @PostMapping(value = "/saveContent")
    @ResponseBody
    public void saveContent(@RequestBody Diary diary, HttpServletRequest request) {
           accountService.saveText(diary);
        RelationTagDiary relationTagDiary = new RelationTagDiary();
        relationTagDiary.setDiaryId(diary.getId());
        String[] split = diary.getTags().split(",");

        for (String s : split) {
            String uuid = UUID.randomUUID().toString();
            relationTagDiary.setId(uuid);
            relationTagDiary.setTagId(s);
            accountService.saveRelationTagDiary(relationTagDiary);

        }


    }

    @GetMapping (value = "/updateContent" ,produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String updateContent(HttpServletResponse response) {

//        Cookie cookie = new Cookie("user_id", "9528");
//        cookie.setMaxAge(60*60*24); // Cookie的存活时间（自定义）
//        cookie.setPath("/"); // 默认路径
//        response.addCookie(cookie);

        return "我是谁";
    }

    @GetMapping("/queryContent/{titleId}")
    @ResponseBody
    public String queryContent(@PathVariable String titleId) {
       return null;
    }
}
